package com.wyj.controller;

import com.wyj.pojo.DiscussPost;
import com.wyj.pojo.Page;
import com.wyj.pojo.User;
import com.wyj.service.DiscussPostService;
import com.wyj.service.UserService;
import com.wyj.service.imp.FollowServiceImp;
import com.wyj.service.imp.LikeServiceImp;
import com.wyj.service.imp.MessageServiceImp;
import com.wyj.util.CommunityConstant;
import com.wyj.util.CommunityUtil;
import com.wyj.util.HostHolder;
import com.wyj.util.ResponseData;
import javafx.beans.binding.ObjectExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController implements CommunityConstant {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Autowired
    ResponseData responseData;

    @Autowired
    LikeServiceImp likeServiceImp;

    @Autowired
    HostHolder hostHolder;

    @Autowired
    FollowServiceImp followServiceImp;

    @Autowired
    MessageServiceImp messageServiceImp;


    //    @ResponseBody
    @GetMapping(path = "/index")
    public String getIndexPage(Model model, Page page,
                            @RequestParam(name = "modelOpt",defaultValue = "0") int modelOpt) {

        /**
         * 这里page.getOffent()是我们表中得起始条数 getLimit是每页多少条数  这个条数是固定的
         * 前端通过 <a href。。。发出连接  然后得话 参数是  有从第几页开始的
         */
        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(),modelOpt);

        /**
         * springmvc会自动实例化Page和Model 并将Page注入Model 所以我们在thymleaf中直接调用
         */
        page.setRows(discussPostService.findDiscussPostRows(0));
        page.setPath("/index?modelOpt="+modelOpt);

        //这个里面存放的数据就是我们一个讨论贴对应一个用户
        List<Map<String, Object>> discussPosts = new ArrayList<>();

        if (list != null) {
            for (DiscussPost post : list) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("post", post);
                //获取到每个讨论贴(每个讨论贴也是一个对象)的发布的用户的id
                User user = userService.getUserById(post.getUserId());

                //根据 entityType跟 entityId的话 我们可以确定一个帖子
                long likeCount = likeServiceImp.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());

                map.put("likeCount", likeCount);

                map.put("user", user);
                discussPosts.add(map);
            }
        }

        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("modelOpt",modelOpt);


        //添加信息的未读数量
        User user = hostHolder.getUser();

        if (user != null) {
            int unReadMessages = messageServiceImp.AllUnReadMessages(user.getId());
            model.addAttribute("AllUnReads",unReadMessages);
        }

        return "/index";
    }

    /**
    * @Description 用户详情面的请求  这里的userId 是某个帖子的Id
    * @param userId
    * @retuen java.lang.String
    * @author wyj
    * @since  2022/7/27 15:41

    */
    @RequestMapping(value = "/profile/{userId}", method = RequestMethod.GET)
    public String getProfile(@PathVariable("userId") int userId,Model model) {
        //当前帖子的主人
        User user = userService.getUserById(userId);
        User loginUser = hostHolder.getUser();


        if (loginUser == null) {
            throw new IllegalArgumentException("您还未登录");
        }

        int userLikeCount = likeServiceImp.findUserLikeCount(userId);

        //这个user存的是该帖子主人的信息
        model.addAttribute("user",user);
        model.addAttribute("userLikeCount",userLikeCount);

        //获取一个人的 粉丝数跟关注列表的数目
        //userId:3 --->关注列表当中的数目
        //3:userId --->粉丝数目
        //获取该帖子用户当中的 关注列表人数
        int focus = followServiceImp.focus(userId, 3);
        //获取该帖子用户的粉丝数目
        int followers = followServiceImp.followers(3, userId);
        //某用户是否关注了某人 (userId 是帖子的主任)
        boolean isfocus = followServiceImp.isFocus(loginUser.getId(), 3, userId);

        model.addAttribute("focus",focus);
        model.addAttribute("followers",followers);
        model.addAttribute("isfocus",isfocus == true ? 0 : 1);

        return "site/profile";
    }


    @RequestMapping(value = "textAjax")
    public String textAjax() {
        return "site/text-ajax";
    }


    /**
     * @param name, age]
     * @Description 描述该方法实现的功能业务逻辑说明调用说明
     * @retuen java.lang.String
     * @author wyj
     * @since 2022/7/12 16:07
     */
    @RequestMapping(value = "/ajax", method = RequestMethod.POST)
    @ResponseBody
    public String textAjax(String name, String sex) {
        System.out.println(name);
        System.out.println(sex);

        //返回相应的数据
        return CommunityUtil.getJsonString(0, "获取成功");
    }

    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public String getError() {
        return "/error/500";
    }

    @RequestMapping(value = "/getUserByIdText/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object getUserByIdText(@PathVariable("id") int id) {
        return ResponseData.success(userService.getUserById(id));
    }


}
